package view;

//SplashScreen.java
//A simple application to show a title screen in the center of the screen
//for the amount of time given in the constructor.  This class includes
//a sample main() method to test the splash screen, but it's meant for use
//with other applications.
//

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.*;

import control.networking.RiskClient;

import model.Map;

public class SplashRiskFrame extends JWindow {

	private static final long serialVersionUID = -7269385239010866684L;
	private static RiskFrame frame;
	private Timer t;
	private Timer a;
	private JLabel label;
	private BufferedImage bigImage;
	private BufferedImage image;
	/**
	 * Splash Screen to introduce the game. Animates by SpriteSheet
	 * @param gameMap
	 * @param rc
	 */
	public SplashRiskFrame(Map gameMap, RiskClient rc ) {
		showSplash(gameMap, rc);
	}

	// A simple little method to show a title screen in the center
	// of the screen for the amount of time given in the constructor
	public void showSplash(Map gameMap, RiskClient rc) {
		JPanel content = (JPanel) getContentPane();
		content.setBackground(Color.white);

		// Set the window's bounds, centering the window
		int width = 769;
		int height = 575;
		Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
		int x = (screen.width - width) / 2;
		int y = (screen.height - height) / 2;
		setBounds(x, y, width, height);

		// Build the splash screen
		label = new JLabel();
//		label.setIcon(new ImageIcon("images/SplashRISK.png"));
		try {
			bigImage = ImageIO
					.read(new File("images/SplashRISK.png"));
		} catch (IOException e) {

		}
		try {
			image = ImageIO
					.read(new File("images/SplashRISKstart.png"));
		} catch (IOException e) {

		}
		Graphics2D g = image.createGraphics();
		g.drawImage(bigImage, 0,0, null);
		label.setIcon(new ImageIcon(image));
		
		
		
		
		
		
		
		JLabel copyrt = new JLabel("CSC 335 Purple-People Eaters:  RISK!  ",
				JLabel.CENTER);
		copyrt.setFont(new Font("Sans-Serif", Font.BOLD, 12));
		content.add(label, BorderLayout.CENTER);
		content.add(copyrt, BorderLayout.SOUTH);
		Color oraRed = new Color(156, 20, 20, 255);
		content.setBorder(BorderFactory.createLineBorder(oraRed, 10));
		
		this.addMouseListener(new MouseActionListener());
		
		// Display splash screen
		setVisible(true);
		
		// set up Timer for animating, start animation
		a = new Timer(700, new AnimationActionListener());
		a.start();
		
		// Make RiskFrame and wait
		frame = new RiskFrame(gameMap, rc);		
		
	}
	public RiskFrame getRiskFrame(){
		return frame;
	}
	private class AnimationActionListener implements ActionListener {

		private int x;
		private int y;
		private int iterations;
		
		public AnimationActionListener(){
			x = 0;
			y = 0;
			iterations = 1;
		}
		@Override
		public void actionPerformed(ActionEvent arg0) {
			if (iterations == 6){
				a.stop();
				if(frame ==  null){
					System.out.println("FRAME NULL");
					iterations --;
					return;
				}
				frame.setVisible(true);
				setVisible(false);
				dispose();
			}
			else if(iterations >=3){
				x = (iterations-3)*750;
				y = 550;
			}
			else {
				x = iterations*750;
				y = 0;
			}
			
			iterations++;
			
			BufferedImage subImage = bigImage.getSubimage(x, y, 750, 550);
			
			Graphics2D g = image.createGraphics();
			g.drawImage(subImage, 0,0, null);
			label.setIcon(new ImageIcon(image));
			validate();
			
			
		}
		
	}
	private class MouseActionListener implements MouseListener {

		@Override
		public void mouseClicked(MouseEvent arg0) {
			if(frame != null){		// wait for frame to be done building
				a.stop();
				frame.setVisible(true);
				setVisible(false);
				dispose();
			}
		}
		@Override
		public void mouseEntered(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseExited(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mousePressed(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseReleased(MouseEvent arg0) {
			// TODO Auto-generated method stub
		}
		
	}
}